package com.newedu.healthtest.utils;

import com.google.common.io.Files;
import io.qameta.allure.Attachment;
import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;

import java.io.File;
import java.io.IOException;

/**
 * @author fujun
 * @create 2021-10-29 09:21:58
 * @company 新开普电子股份有限公司
 */
public class AllureReportListener implements IHookable {
    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        callBack.runTestMethod(testResult);
        if(testResult.getThrowable() !=null){
            try{
                // *þ
                saveScreenShot();
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
    }

    @Attachment(value = "page screenshot", type = "image/jpg")
    public byte[] saveScreenShot() throws IOException {
        String fileName = System.currentTimeMillis() + ".jpg";
        File file  = DriverUtils.takeScreenShot("./pictures/"+fileName);
        return Files.toByteArray(file);
    }
}
